package sm.pzavoli.mercato.model.db;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;
import sm.pzavoli.mercato.MercatoDBOpenHelper;
import sm.pzavoli.mercato.model.Acquisto;
import sm.pzavoli.mercato.model.Ticker;
import sm.pzavoli.std.DMC;
import sm.pzavoli.std.Lista;

public class TickerDAO {

	public static Cursor query(MercatoDBOpenHelper db, String symbol, 
			String orderby) {
		try {
	        if (db == null) {
	            Log.e("TicketDAO", "db is null");
	            return null;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        qb.setTables(MercatoDBOpenHelper.WATCHLIST_TABLE);
	        String selection = "";
	        if ( symbol != null && symbol.length() > 0 ) {
	        	selection = db.KEY_SYMBOL+" = '"+symbol.trim()+"'";
	        }
	        Cursor c = qb.query(db.getReadableDatabase(),
	                null,
	                selection, null,
	                null, null, orderby);
	        return c;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TicketDAO", "Errore in apertura cursore");
			return null;
		}
	}

	public static List<Ticker> queryTicker(MercatoDBOpenHelper db, String symbol, 
			String orderby) {
		try {
	        if (db == null) {
	            Log.e("TicketDAO", "db is null");
	            return null;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        qb.setTables(MercatoDBOpenHelper.WATCHLIST_TABLE);
	        String selection = "";
	        if ( symbol != null && symbol.length() > 0 ) {
	        	selection = db.KEY_SYMBOL+" = '"+symbol.trim()+"'";
	        }
	        Cursor c = qb.query(db.getReadableDatabase(),
	                null,
	                selection, null,
	                null, null, orderby);
	        ArrayList<Ticker> lista = new ArrayList<Ticker>();
	        while ( c.moveToNext()) {
	        	Ticker t = new Ticker(c.getInt(0),c.getString(3),c.getString(2));
	        	lista.add(t);
	        }
	        c.close();
	        return lista;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TicketDAO", "Errore in apertura cursore");
			return null;
		}
	}

	
	public static boolean insert(MercatoDBOpenHelper db, Ticker ticker) {
		try {
	        if (db == null) {
	            Log.e("TickerDAO", "db is null");
	            return false;
	        }
	        SQLiteDatabase database = db.getWritableDatabase();
	        ContentValues values = new ContentValues();
	        values.put(db.KEY_SYMBOL, ticker.getSymbol());
	        values.put(db.KEY_NOME, ticker.getNome());
	        long id = database.insert(db.WATCHLIST_TABLE,null,values);
	        if ( id < 0) 
	        	return false;
	        ticker.setID((int)id);
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TickerDAO", "Errore in insert:"+e.getMessage());
			return false;
		}
	}
	
	public static boolean delete(MercatoDBOpenHelper db, Ticker ticker) {
		try {
	        if (db == null) {
	            Log.e("TickerDAO.delete", "db is null");
	            return false;
	        }
	        SQLiteDatabase database = db.getWritableDatabase();
	        String where = "" + Ticker.CSZ_ID + " = "+ticker.getID();
	        long id = database.delete(db.WATCHLIST_TABLE,where,null);
	        if ( id < 0) 
	        	return false;
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TickerDAO.delete", "Errore in delete:"+e.getMessage());
			return false;
		}
	}
	
	public static Lista leggiAcquistoTicker(MercatoDBOpenHelper db, int id) {
		try {
	        if (db == null) {
	            Log.e("TicketDAO", "db is null");
	            return null;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        Lista lista = new Lista(Acquisto.class);
	        DMC dmc = new DMC(db);
	        String select = "select * from "+db.ACQUISTI_TABLE+" where "+db.KEY_ID_TICKER+" = "+id;
	        if ( ! dmc.load(lista, select)) {
	        	return null;
	        }
	        return lista;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TicketDAO", "Errore in apertura cursore");
			return null;
		}		
	}

	public static Lista leggiTickers(MercatoDBOpenHelper db, int id) {
		try {
	        if (db == null) {
	            Log.e("TicketDAO", "db is null");
	            return null;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        Lista lista = new Lista(Ticker.class);
	        DMC dmc = new DMC(db);
	        String select = "select * from "+db.WATCHLIST_TABLE+" where 1=1";
	        if ( ! dmc.load(lista, select)) {
	        	return null;
	        }
	        return lista;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TicketDAO", "Errore in apertura cursore");
			return null;
		}		
	}

	public static boolean leggiTickers(MercatoDBOpenHelper db, int id, Lista ls) {
		try {
	        if (db == null) {
	            Log.e("TicketDAO", "db is null");
	            return false;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        if ( ls == null)
	        	ls = new Lista(Ticker.class);
	        ls.clear();
	        DMC dmc = new DMC(db);
	        String select = "select * from "+db.WATCHLIST_TABLE+" where 1=1";
	        if ( ! dmc.load(ls, select)) {
	        	return false;
	        }
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TicketDAO", "Errore in apertura cursore");
			return false;
		}		
	}
	
}
